package com.junjie.checkin.security.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.junjie.checkin.response.ResultStatus;
import com.junjie.checkin.response.Result;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 洪浚杰
 * @description
 * @date 2025/6/9 19:04
 */
public class PermissionHandle implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.getOutputStream().write(
                new ObjectMapper().writeValueAsBytes(Result.getResult(ResultStatus.ACCESS_DENIED))
        );
    }
}
